//
//  JKShopsVC.m
//  chequanbao_ios
//
//  Created by 朱凯 on 2018/1/29.
//  Copyright © 2018年 com.itboye. All rights reserved.
//

#import "JKShopsVC.h"

// vc
#import "JKShopsDetailVC.h"
#import "ByUIShopsIndexVC.h"

// view
#import "JKViewShopsSections.h"
#import "ByViewDetailHeaderCarousel.h"
#import "JKViewFriendsSegment.h"
#import "JKViewShopsProducts.h"

// api
#import "ByApiShops.h"
#import "ByApiAppIndex.h"

// model
#import "ByModelShopCate.h"
#import "ByModelIndexCarousel.h"

@interface JKShopsVC ()

@property (nonatomic, strong) JKViewShopsSections *sectionsView;
@property (nonatomic, strong) ByViewDetailHeaderCarousel *carousel;
@property (nonatomic, strong) JKViewFriendsSegment *segments;
@property (nonatomic, strong) JKViewShopsProducts *productsView;

@property (nonatomic, strong) NSArray *level1Cates; // 一级类目，当前写死
@property (nonatomic, strong) NSArray *level2Cates; // 二级，根据1级来
@property (nonatomic, strong) NSArray *level3Cates; // 三级，根据2级来
@end

@implementation JKShopsVC

#pragma mark - 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[self.view viewWithTag:888] removeFromSuperview];
    self.title = @"折扣商家";
    [self addColorLeftBackBarButtonItem];
    [self createViews];
    
    [self getCates];
    [self getCarousel];

}
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    [self setNaviAttributeWithTranslucent:false stateBarLightContent:false];
}

#pragma mark - 创建视图
- (void)getCates {
    
    ByNetParamsModel *model = [[ByNetParamsModel alloc] init];
    [ByApiShops getShopCate:model success:^(ByNetBaseResult *result) {
        _level1Cates = result.data;
        
        ByModelShopCate *cate = _level1Cates.firstObject;
        _level2Cates = cate.children;
        NSMutableArray *temp = [NSMutableArray new];
        [cate.children enumerateObjectsUsingBlock:^(ByModelShopCate *model, NSUInteger idx, BOOL * _Nonnull stop) {
            [temp addObject:model.name];
        }];
        _segments.segments = temp.copy;
        [_segments mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@(temp.count * 100));
        }];
        
        cate = _level2Cates.firstObject;
        _level3Cates = cate.children;
        _productsView.products = _level3Cates;
        
    } failure:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showOnlyText:result.msg];
    }];
}
- (void)getCarousel {
    
    ByNetParamsModel *params = [ByNetParamsModel new];
    params.position = @"6248";
    [ByApiAppIndex getIndexCarousel:params success:^(ByNetBaseResult *result) {
        self.carousel.imageURLStringDatas = result.data;
    } failure:^(ByNetBaseResult *result) {
        _carousel.imageURLStringDatas = nil;
    }];
}

#pragma mark - 创建视图
- (void)createViews {
    
    _sectionsView = [JKViewShopsSections new];
    [self.view addSubview:_sectionsView];
    [_sectionsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(@0);
    }];
    
    UIScrollView *scroll = [UIScrollView new];
    [self.view addSubview:scroll];
    [scroll mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_sectionsView.mas_bottom).offset(SCREEN_WIDTH * 0.25);
        make.left.right.equalTo(@0);
        make.height.equalTo(@60);
    }];
    
    _segments = [JKViewFriendsSegment new];
    _segments.backgroundColor = [UIColor clearColor];
    [scroll addSubview:_segments];
    [_segments mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(scroll);
        make.left.top.equalTo(@0);
        
        make.right.equalTo(@0);
    }];
    
    _productsView = [JKViewShopsProducts new];
    [self.view addSubview:_productsView];
    [_productsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@-10);
        make.left.equalTo(@10);
        make.top.equalTo(scroll.mas_bottom);
    }];
    
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.carousel = [[ByViewDetailHeaderCarousel alloc] initWithFrame:CGRectMake(0, _sectionsView.bottom, SCREEN_WIDTH, SCREEN_WIDTH * 0.25) timerDuration:3];
        self.carousel.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
        [self.view addSubview:self.carousel];
        __weak typeof(self) wself = self;
        _carousel.itemDidTap = ^(NSInteger index) {
            ByModelIndexCarousel *model = wself.carousel.imageURLStringDatas[index];
            if (![model.storeid isEqualToString:@"-1"] && ![model.storeid isEqualToString:@"0"] && model.storeid.length != 0) {
                JKShopsDetailVC *vc = [[JKShopsDetailVC alloc] init];
                vc.seller_id = model.storeid;
                [wself by_normalPush:vc hasNavi:YES];
            }
        };
    });
    
    
    // 点击
    __weak typeof(self) wself = self;
    _sectionsView.selectBlock = ^(NSInteger index) {
        [wself sectionTap:index];
    };
    _segments.selectBlock = ^(NSInteger index) {
        [wself segmentTap:index];
    };
    _productsView.selectBlock = ^(NSInteger index) {
        [wself productTap:index];
    };
}

#pragma mark - 事件
- (void)sectionTap:(NSInteger)index {
    
    // 其他
    if (index == 4) {
        ByUIShopsIndexVC *vc = [[ByUIShopsIndexVC alloc] init];
        [self by_normalPush:vc hasNavi:YES];
    }
    else {
        ByModelShopCate *cate = _level1Cates[index];
        _level2Cates = cate.children;
        NSMutableArray *temp = [NSMutableArray new];
        [cate.children enumerateObjectsUsingBlock:^(ByModelShopCate *model, NSUInteger idx, BOOL * _Nonnull stop) {
            [temp addObject:model.name];
        }];
        _segments.segments = temp.copy;
        [_segments mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@(temp.count * 100));
        }];
        
        cate = _level2Cates.firstObject;
        _level3Cates = cate.children;
        _productsView.products = _level3Cates;
    }
}
- (void)segmentTap:(NSInteger)index {
    
    ByModelShopCate *cate = _level2Cates[index];
    _level3Cates = cate.children;
    _productsView.products = _level3Cates;
}
- (void)productTap:(NSInteger)index {
    
    ByUIShopsIndexVC *vc = [[ByUIShopsIndexVC alloc] init];
    ByModelShopCate *cate = _level3Cates[index];
    vc.cate_id = cate.cate_id;
    [self by_normalPush:vc hasNavi:YES];

}

#pragma mark - 


@end
